import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import useStore1 from '@/store/store1'
const routes = [
    {
        path: '/',
        name: "home", //以后跳转用的名称
        component: () => import("@/views/Home.vue"),
        meta: { title: '首页', icon: 'HomeFilled' ,show:true,roles:['admin','producter','purchaser.manager','purchaser']}
    },
    {
        path: '/category',
        name: "category",
        component: () => import("@/views/category/Index.vue"),
        meta: { title: '品类管理', icon: 'Tickets' ,roles:['admin','producter']},
        children: [
            {
                path: '/category/list',
                name: "category-list",
                component: () => import("@/views/category/List.vue"),
                meta: { title: '品类列表', icon: 'Memo' ,roles:['admin','producter']},
            },
            {
                path: '/category/add',
                name: "category-add",
                component: () => import("@/views/category/Add.vue"),
                meta: { title: '添加品类', icon: 'CirclePlusFilled' ,roles:['admin','producter']},
            },
            {
                path: '/category/edit',
                name: "category-edit",
                component: () => import("@/views/category/Edit.vue"),
                meta: { title: '品类修改', icon: 'CirclePlusFilled',show:false,roles:['admin','producter']}
            }
        ]
    },
    {
        path: '/product',
        name: "product",
        component: () => import("@/views/product/Index.vue"),
        meta: { title: '商品管理', icon: 'Tickets' ,roles:['admin','producter']},
        children: [
            {
                path: '/product/list',
                name: "product-list",
                component: () => import("@/views/product/List.vue"),
                meta: { title: '商品列表', icon: 'Memo' ,roles:['admin','producter']},
            },
            {
                path: '/product/add',
                name: "product-add",
                component: () => import("@/views/product/Add.vue"),
                meta: { title: '商品添加', icon: 'CirclePlusFilled' ,roles:['admin','producter']},
            },
            {
                path: '/product/edit',
                name: "product-edit",
                component: () => import("@/views/product/Edit.vue"),
                meta: { title: '商品修改', icon: 'CirclePlusFilled',show:false,roles:['admin','producter']},
            }
        ]
    },
    {
        path: '/vendor',
        name: "vendor",
        component: () => import("@/views/vendor/Index.vue"),
        meta: { title: '供应商管理', icon: 'Tickets',roles:['admin','manager','purchaser'] },
        children: [
            {
                path: '/vendor/list',
                name: "vendor-list",
                component: () => import("@/views/vendor/List.vue"),
                meta: { title: '供应商列表', icon: 'Memo',roles:['admin','purchaser.manager','purchaser'] },
            },
            {
                path: '/vendor/add',
                name: "vendor-add",
                component: () => import("@/views/vendor/Add.vue"),
                meta: { title: '供应商添加', icon: 'CirclePlusFilled' ,roles:['admin','purchaser.manager','purchaser']},
            },
            {
                path: '/vendor/edit',
                name: "vendor-edit",
                component: () => import("@/views/vendor/Edit.vue"),
                meta: { title: '供应商修改', icon: 'CirclePlusFilled',show:false,roles:['admin','purchaser.manager','purchaser']},
            }
        ]
    },
    {
        path: '/warehouse',
        name: 'warehouse',
        component: () => import("@/views/warehouse/Index.vue"),
        meta: { title: '仓库管理', icon: 'Tickets',  roles:['admin','producter']},
        children: [
            {
                path: '/warehouse/list',
                name: 'warehouse-list',
                component: () => import("@/views/warehouse/List.vue"),
                meta: { title: '库房管理', icon: 'Memo',  roles:['admin','producter'] }
            },
            {
                path: '/warehouse/shelf',
                name: 'warehouse-shelf',
                component: () => import("@/views/warehouse/Shelf.vue"),
                meta: { title: '货架管理', icon: 'CirclePlusFilled' ,  roles:['admin','producter']}
            },
            {
                path: '/warehouse/edit',
                name: 'warehouse-edit',
                component: () => import("@/views/warehouse/Edit.vue"),
                meta: { title: '编辑', icon: 'CirclePlusFilled',show: false, }
            }
        ]
    },
    {
        path: '/po',
        name: "po",
        component: () => import("@/views/po/Index.vue"),
        meta: { title: '采购单管理', icon: 'Tickets' ,roles:['admin','purchaser.manager','purchaser']},
        children: [
            {
                path: '/po/list',
                name: "po-list",
                component: () => import("@/views/po/List.vue"),
                meta: { title: '采购单列表', icon: 'Memo',roles:['admin','purchaser.manager','purchaser'] },
            },
            {
                path: '/po/add',
                name: "po-add",
                component: () => import("@/views/po/Add.vue"),
                meta: { title: '采购单添加', icon: 'CirclePlusFilled' ,roles:['admin','purchaser.manager','purchaser']},
            },
            {
                path: '/po/edit',
                name: "po-edit",
                component: () => import("@/views/po/Edit.vue"),
                meta: { title: '采购单详情', icon: 'CirclePlusFilled',show:false,roles:['admin','purchaser.manager','purchaser']},
            }
        ]
    },
    {
        path: '/io',
        name: "io",
        component: () => import("@/views/io/Index.vue"),
        meta: { title: '入库单管理', icon: 'Tickets' ,roles:['admin','purchaser.manager','purchaser']},
        children: [
            {
                path: '/io/list',
                name: "io-list",
                component: () => import("@/views/io/List.vue"),
                meta: { title: '入库单列表', icon: 'Memo',roles:['admin','purchaser.manager','purchaser'] },
            }
        ]
    },
	{
		path: '/oo',
		name: 'oo',
		component: () => import('@/views/oo/Index.vue'),
		meta: {
			title: '出库单',
			icon: 'Tickets',
			roles: ['admin', 'purchaser', 'purchaser.manager'],
		},
		children: [
			{
				path: '/oo/list',
				name: 'oo-list',
				component: () => import('@/views/oo/List.vue'),
				meta: {
					title: '出库单列表',
					icon: 'List',
					roles: ['admin', 'purchaser', 'purchaser.manager'],
				},
			},
			{
				path: '/oo/add',
				name: 'oo-add',
				component: () => import('@/views/oo/Add.vue'),
				meta: {
					title: '添加出库单',
					icon: 'Plus',
					roles: ['admin', 'purchaser', 'purchaser.manager'],
				},
			}
		],
	},
    {
        path: '/user',
        name: "user",
        component: () => import("@/views/user/Index.vue"),
        meta: { title: '用户', icon: 'Tickets',roles:'admin',show: false,},
        children: [
            {
                path: '/user/login',
                name: "user-login",
                component: () => import("@/views/user/Login.vue"),
                meta: { title: '登录', icon: 'Menu',show:false,useFrame:false}
            },
            { 
                path: '/user/register',
                name: "user-register",
                component: () => import("@/views/user/Register.vue"),
                meta: { title: '注册', icon: 'Menu',show:false,useFrame:false}
            }
        ]
    },
	{
		path: '/role',
		name: 'role',
		component: () => import('@/views/role/Index.vue'),
		meta: {
			title: '权限管理',
			icon: 'UserFilled',
			roles: ['admin'],
		},
		children: [
			{
				path: '/role/User',
				name: 'role-list',
				component: () => import('@/views/role/User.vue'),
				meta: {
					title: '用户列表',
					icon: 'List',
					roles: ['admin'],
				},
			},
			{
				path: '/role/Role',
				name: 'role-add',
				component: () => import('@/views/role/Role.vue'),
				meta: {
					title: '角色列表',
					icon: 'Compass',
					roles: ['admin'],
				},
			},
			{
				path: '/role/permissions',
				name: 'role-permissions',
				component: () => import('@/views/role/Permissions.vue'),
				meta: {
					title: '权限列表',
					icon: 'TurnOff',
					roles: ['admin'],
				},
			},
		],
	},

]

const router = createRouter({
    // history: createWebHashHistory('/'),
    history: createWebHistory(),
    routes
})

// 从哪个页面跳转到哪个页面
router.beforeEach((to,from,next)=>{
    // console.log(from)
    // console.log(to)
    // next()

    const store1 = useStore1()
    if(to.name != "user-login" && to.name != "user-register"){
        if(store1.token === ""){
            next('/user/login')
        }else{
            next()
        }
    }
    else{
        next()
    }
})
export default router